<template>
  <el-button type="primary" plain @click="dialogFormVisible = true">添加学生</el-button>
  <el-table :data="students" style="width: 100%">
    <el-table-column label="学号" prop="id" />
    <el-table-column label="姓名" prop="name" />
    <el-table-column label="性别" prop="gender" />
    <el-table-column label="年龄" prop="age" />
    <el-table-column label="手机" prop="cellphone" />
    <el-table-column label="籍贯" prop="address" />
    <el-table-column align="right">
      <template #header>
        <el-input v-model="keyword" size="small" placeholder="搜索学生" />
      </template>
      <template #default="scope">
        <el-button size="small" @click="editStudent(scope.row)">
          更新
        </el-button>
        <el-button
            size="small"
            type="danger"
            @click="deleteStudent(scope.row.id)">
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <!--  分页-->
  <el-pagination background layout="prev, pager, next" :total="total"
                 :page-size="pageSize" @current-change="loadStudent"/>
  <!--  对话框-->
  <el-dialog v-model="dialogFormVisible" title="学生信息" width="500" @close="student = {}">
    <el-form :model="student">
      <el-form-item label="姓名" >
        <el-input v-model="student.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="年龄" >
        <el-input v-model="student.age" autocomplete="off" />
      </el-form-item>
      <el-form-item label="性别" >
        <el-input v-model="student.gender" autocomplete="off" />
      </el-form-item>
      <el-form-item label="电话" >
        <el-input v-model="student.cellphone" autocomplete="off" />
      </el-form-item>
      <el-form-item label="籍贯" >
        <el-input v-model="student.address" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="saveStudent">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { ref, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from "element-plus";
import http from "@/utils/request";

export default {
  name: "StudentView",
  setup() {
    const students = ref([]);
    const keyword = ref('');
    const current = ref(1);
    const pageSize = ref(10);
    const total = ref(10);
    const dialogFormVisible = ref(false);
    const student = ref({});

    const loadStudent = (cur) => {
      current.value = cur
      http.get('/findStudentPage?current=' + current.value + "&pageSize=" + pageSize.value)
          .then(result => {
            console.log('student', result.data);
            if (result.data.code === 200) {
              students.value = result.data.data.data;
              total.value = result.data.data.total;
            }
          });
    };

    const editStudent = (stu) => {
      dialogFormVisible.value = true;
      student.value = JSON.parse(JSON.stringify(stu));
    };

    const saveStudent = () => {
      if (student.value.id) {
        http.post('/updateStudent', this.$qs.stringify(student.value))
            .then(result => {
              console.log('update', result.data);
              if (result.data.code === 200) {
                ElMessage.success('更新成功');
                loadStudent(current.value);
              }
            });
      } else {
        http.post('/addStudent', this.$qs.stringify(student.value))
            .then(result => {
              console.log('add', result.data);
              if (result.data.code === 200) {
                ElMessage.success('添加成功');
                loadStudent(current.value);
              }
            });
      }
      dialogFormVisible.value = false;
    };

    const deleteStudent = (id) => {
      ElMessageBox.confirm(
          '是否确认删除?',
          'Warning',
          {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          }
      ).then(() => {
        http.post('/deleteStudent?id=' + id)
            .then(result => {
              if (result.data.code === 200) {
                loadStudent(current.value);
              }
            });
      }).catch(() => {});
    };

    onMounted(() => {
      loadStudent(1);
    });

    return {
      students,
      keyword,
      current,
      pageSize,
      total,
      dialogFormVisible,
      student,
      loadStudent,
      editStudent,
      saveStudent,
      deleteStudent,
    };
  },
};
</script>

<style scoped>

</style>
